package com.demo.common.redis.configure;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置
 *
 * @author wr
 */
@Configuration
@EnableCaching
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport {

//    private final Environment environment;

//    public RedisConfig(Environment environment) {
//        this.environment = environment;
//    }

    /**
     * 此处是加密配置，在yml中配置加密redis密码。此处将密码解密并设值。
     * @param connectionFactory
     * @return
     */
//    @Bean
//    public RedisConnectionFactory myLettuceConnectionFactory() {
//        String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAiLQsbu3qcqCEb0wNknAtJ1u2W6OLx4BlNHvEJidM1aijCu6u8vGQ1Ucm4ZttoW6qfpICeRytaZbTcqTLnpZfPQIDAQABAkARethjptBfr96/TH4/x1A+xB9hQFxTOju2pkzChCvy9dhwOF7HU4VYiSwjR3APjQVe3L2DWkoP8qU/LqD7O7gRAiEA5Qf+ADOjbW26VkHSH3rvIjelMMZEqYmdYdhEQos3QXMCIQCYzQbz61j+QVkbBw3/2pTjcEjxd3Z1sWVtow6Jge/wjwIgSX1gBmaLaSCNawIhz1IeC2e5jMYB4Di1FGFVvdh3+XsCIE/axolvM4EdEb5IP4644xrBsb1P237CiOl1T8GZQJYNAiEA3qGdRqWJKYoJOszuassOrMMgATlNdUBAXw0EdQk34ug=";
//        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(environment.getProperty("spring.redis.host"), Integer.parseInt(environment.getProperty("spring.redis.port")));
//        redisStandaloneConfiguration.setDatabase(Integer.parseInt(environment.getProperty("spring.redis.database")));
//        //获取application.yml 中的密码（密文）
//        String password = environment.getProperty("spring.redis.password");
//        //解密密码并停驾到配置中
//        redisStandaloneConfiguration.setPassword(RsaUtil.decipher(password, privateKey));
//        return new LettuceConnectionFactory(redisStandaloneConfiguration);
//    }

    @Bean
    @SuppressWarnings(value = {"unchecked", "rawtypes"})
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);

        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }
}
